package com.xiaoyu.test;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;

/**
 * ${DESCRIPTION}
 *
 * @author muxiaoyu
 * @create 2017/05/08 17:08
 **/
public class MQSender {
    private final static String QUEUE_NAME = "hello";

    @Test
    public void test01() throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("172.16.11.113");
        //配置使用password登录则必须要设置用户名密码
        connectionFactory.setUsername("xiaoyu");
        connectionFactory.setPassword("123123");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        for (int i = 0; i < 5; i++) {
            String msg = "hello world" + i + " by xiaoyu";
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            System.out.println("[x] Send'" + msg + " '");

        }
        channel.close();
        connection.close();
    }
}

